package com.guodx.study.springboot.d10.framework.async.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
@Slf4j
public class AsyncTaskDemo {

    @Async
    public void runTaskWithoutReturn(int i){
        log.info("返回值为void类型的task异步调用开始, 线程名: {}", Thread.currentThread());
        try {
            Thread.sleep(1000L * i);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("返回值为void类型的task异步调用结束, 线程名: {}", Thread.currentThread());
    }

    @Async
    public Future<Integer> runTaskWithReturn(int i){
        log.info("返回值为int类型的task异步调用开始, 线程名: {}", Thread.currentThread());
        Future<Integer> future = null;
        try {
            Thread.sleep(1000L * i);
            future = new AsyncResult<>(i);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("返回值为int类型的task异步调用结束, 线程名: {}", Thread.currentThread());
        return future;
    }

    @Async
    public void runTaskWithException(int i){
        int res = 1 / i;
        log.info("res: {}", res);
    }

}
